import 'dart:io';
import 'package:common_utils/common_utils.dart';
import 'package:fate_blind_box/chat_view/ahu_text.dart';
import 'package:fate_blind_box/chat_view/circle_progress.dart';
import 'package:fate_blind_box/config/app_style.dart';
import 'package:fate_blind_box/routes/page_route_names.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';

class VideoMessageData extends StatelessWidget {
  final EMVideoMessageBody videoMessage;
  final EMMessage message;
  final EMUserInfo? userInfo;

  const VideoMessageData({
    super.key,
    required this.message,
    required this.videoMessage,
    this.userInfo,
  });

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic>? map = message.attributes;
    int pro = 0;
    if (map != null) {
      pro = map['progress'];
    }
    if (videoMessage.remotePath == null) {
      return InkWell(
        onTap: () {
          EasyLoading.showToast('视频下载中');
        },
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            progressView(pro),
            SizedBox(width: 2.w),
            Stack(
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(6.w),
                  child: Image.file(
                    File('${videoMessage.thumbnailLocalPath}'),
                    width: 180.w,
                    height: 120.w,
                    fit: BoxFit.fill,
                  ),
                ),
                Positioned(
                    left: (180 / 2 - 22).w,
                    top: (120 / 2 - 22).w,
                    child: Image.asset(
                      'images/play.png',
                      width: 44.w,
                      height: 44.w,
                      color: Colors.white,
                    )),
                Positioned(
                    bottom: 6.w,
                    right: 6.w,
                    child: Container(
                        padding: EdgeInsets.symmetric(
                            horizontal: 6.w, vertical: 3.w),
                        decoration: BoxDecoration(
                          color: Colors.black.withOpacity(0.65),
                          borderRadius: BorderRadius.circular(4.w),
                        ),
                        child: AhuText(
                          text: _durationTransform(videoMessage.duration!),
                          color: Colors.white,
                          fontSize: 9.sp,
                        ))),
              ],
            ),
          ],
        ),
      );
    } else {
      return groupWidget(pro);
    }
  }

  Widget groupWidget(int pro) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // message.direction == MessageDirection.RECEIVE
        //     ? personWidget()
        //     : const SizedBox(),
        // SizedBox(
        //     height: message.direction == MessageDirection.RECEIVE ? 6.w : 0),
        InkWell(
          onTap: () {
            Get.toNamed(PageRouteNames.videoPlayer, arguments: {
              'message': message,
            });
          },
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              progressView(pro),
              SizedBox(width: 2.w),
              Stack(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(6.w),
                    child: Image.network(
                      '${videoMessage.thumbnailRemotePath}',
                      width: 180.w,
                      height: 120.w,
                      fit: BoxFit.fill,
                    ),
                  ),
                  Positioned(
                      left: (180 / 2 - 22).w,
                      top: (120 / 2 - 22).w,
                      child: Image.asset(
                        'images/play.png',
                        width: 44.w,
                        height: 44.w,
                        color: Colors.white,
                      )),
                  Positioned(
                      bottom: 6.w,
                      right: 6.w,
                      child: Container(
                          padding: EdgeInsets.symmetric(
                              horizontal: 6.w, vertical: 3.w),
                          decoration: BoxDecoration(
                            color: Colors.black.withOpacity(0.65),
                            borderRadius: BorderRadius.circular(4.w),
                          ),
                          child: AhuText(
                            text: _durationTransform(videoMessage.duration!),
                            color: Colors.white,
                            fontSize: 9.sp,
                          ))),
                ],
              ),
            ],
          ),
        )
      ],
    );
  }

  /// 成员信息
  Widget personWidget() {
    if (userInfo?.gender == 2) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Container(
            padding: EdgeInsets.symmetric(vertical: 2.w),
            margin: EdgeInsets.only(right: 4.w),
            decoration: BoxDecoration(
              color: '#FFF3FD'.stringToColor,
              borderRadius: BorderRadius.circular(8.w),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(width: 6.w),
                Image.asset(
                  'images/user/txl_woman_icon.png',
                  width: 12.w,
                  height: 12.w,
                ),
                SizedBox(width: 3.w),
                AhuText(
                  text: '${userInfo?.birth}',
                  fontSize: 12,
                  color: '#FE73CF'.stringToColor,
                  fontWeight: FontWeight.w400,
                ),
                SizedBox(width: 6.w),
              ],
            ),
          ),
          AhuText(
            text: '${userInfo?.nickName}',
            color: '#868686'.stringToColor,
            fontSize: 11,
            fontWeight: FontWeight.w400,
          )
        ],
      );
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.symmetric(vertical: 2.w),
          margin: EdgeInsets.only(right: 4.w),
          decoration: BoxDecoration(
            color: '#F3F7FF'.stringToColor,
            borderRadius: BorderRadius.circular(8.w),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(width: 6.w),
              Image.asset(
                'images/user/txl_man_icon.png',
                width: 12.w,
                height: 12.w,
              ),
              SizedBox(width: 3.w),
              AhuText(
                text: '${userInfo?.birth}',
                fontSize: 12,
                color: '#739BFE'.stringToColor,
                fontWeight: FontWeight.w400,
              ),
              SizedBox(width: 6.w),
            ],
          ),
        ),
        AhuText(
          text: '${userInfo?.nickName}',
          color: '#868686'.stringToColor,
          fontSize: 11,
          fontWeight: FontWeight.w400,
        )
      ],
    );
  }

  /// 进度winget
  Widget progressView(int pro) {
    if (pro == 0 || pro == 100) {
      return const SizedBox();
    } else {
      return SizedBox(
        width: 20.w,
        height: 20.w,
        child: CustomPaint(
          painter: CircleProgressBarPainter(
            progress: pro / 100,
            backgroundColor: Colors.transparent,
          ),
        ),
      );
    }
  }

  //时间转换 将秒转换为小时分钟
  String _durationTransform(int seconds) {
    return DateUtil.formatDate(
        DateTime.fromMillisecondsSinceEpoch(seconds * 1000),
        format: 'mm:ss');
  }
}
